package com.ruoyi.config.job;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
1、Maven 依赖：引入 SpringBoot Quartz Starter
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>
2、Quartz 配置：定义 JobDetail 和 Trigger
3、Job 实现：创建具体执行任务的 Job 类
*/
//@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail lotAddDeviceRunDetailsJobDetail() {
        return JobBuilder.newJob(LotAddDeviceRunDetailsJob.class)
                //为 Job 指定唯一标识
                .withIdentity("LotAddDeviceRunDetailsJob")
                //即使没有Trigger关联，也让Job保存在Quartz的JobStore中
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger lotAddDeviceRunDetailsJobTrigger() {
        //简单调度器，支持固定频率执行
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                //每10秒执行一次的定时任务
                .withIntervalInSeconds(5)
                //任务无限循环执行
                .repeatForever();
        return TriggerBuilder.newTrigger()
                //将 Trigger 与 JobDetail 关联
                .forJob(lotAddDeviceRunDetailsJobDetail())
                .withIdentity("lotAddDeviceRunDetailsJobTrigger")
                .withSchedule(scheduleBuilder)
                .build();
    }


    @Bean
    public JobDetail lotSynDeviceIsOnlineJobDetail() {
        return JobBuilder.newJob(LotSynDeviceIsOnlineJob.class)
                //为 Job 指定唯一标识
                .withIdentity("LotSynDeviceIsOnlineJob")
                //即使没有Trigger关联，也让Job保存在Quartz的JobStore中
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger lotSynDeviceIsOnlineJobTrigger() {
        //简单调度器，支持固定频率执行
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                //每10秒执行一次的定时任务
                .withIntervalInSeconds(5)
                //任务无限循环执行
                .repeatForever();
        return TriggerBuilder.newTrigger()
                //将 Trigger 与 JobDetail 关联
                .forJob(lotSynDeviceIsOnlineJobDetail())
                .withIdentity("lotSynDeviceIsOnlineJobTrigger")
                .withSchedule(scheduleBuilder)
                .build();
    }
}
